package com.baizhi.controller;

import com.baizhi.entity.CmfzArticle;
import com.baizhi.luceneindex.LuceneService;
import com.baizhi.service.CmfzArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Controller
@RequestMapping("/article")
public class CmfzArticleController {
    @Autowired
    private CmfzArticleService cmfzArticleService;
    @Autowired
    private LuceneService luceneService;

    @RequestMapping("/selectCmfzArticleDatagrid")
    @ResponseBody
    public Map selectCmfzArticleDatagrid(int page,int rows){
        return cmfzArticleService.selectCmfzArticleDatagrid(page,rows);
    }

    @RequestMapping("/addArticle")
    @ResponseBody
    public Map addArticle(MultipartFile mf, CmfzArticle cmfzArticle, HttpSession session){

        Map map=new HashMap();
        //获取文件路径
        String path=session.getServletContext().getRealPath("/img/");
        //若目录不存在，创建该目录
        File file=new File(path);
        if(!file.exists()){
            file.mkdirs();
        }
        //获取老名字
        String oldName=mf.getOriginalFilename();
        String newName= UUID.randomUUID().toString()+oldName.substring(oldName.lastIndexOf("."));
        //相对路径
        String realPath="/img/"+newName;
        //绝对路径
        String absolPath=session.getServletContext().getRealPath(realPath);

        try {
            mf.transferTo(new File(absolPath));
            //把拍轮播图对象添加到数据库表里面
            cmfzArticle.setArticleImage(realPath);

            //测试是否从页面接收到数据
            System.out.println(cmfzArticle);


            cmfzArticleService.addArticle(cmfzArticle);
            map.put("isAdd",true);
        } catch (IOException e) {
            e.printStackTrace();
            map.put("isAdd",false);
        }
        return map;
    }

    @RequestMapping("/deleteSomeArticles")
    @ResponseBody
    public Map deleteSomeArticles(int[] ids){
        Map map=new HashMap();
        try {
            cmfzArticleService.deleteSomeArticles(ids);
            map.put("isDelete",true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("isDelete",false);
        }
        return map;
    }


    @RequestMapping("/updateArticle")
    @ResponseBody
    public Map updateArticle(MultipartFile umf, int articleId, String articleName, String articleContent, int guruId, Date articleDate, HttpSession session){

        CmfzArticle cmfzArticle=new CmfzArticle();
        cmfzArticle.setArticleId(articleId);
        cmfzArticle.setArticleName(articleName);
        cmfzArticle.setArticleContent(articleContent);
        cmfzArticle.setGuruId(guruId);
        cmfzArticle.setArticleDate(articleDate);

        Map map=new HashMap();
        //获取文件路径
        String path=session.getServletContext().getRealPath("/img/");
        //若目录不存在，创建该目录
        File file=new File(path);
        if(!file.exists()){
            file.mkdirs();
        }
        //获取老名字
        String oldName=umf.getOriginalFilename();
        String newName= UUID.randomUUID().toString()+oldName.substring(oldName.lastIndexOf("."));
        //相对路径
        String realPath="/img/"+newName;
        //绝对路径
        String absolPath=session.getServletContext().getRealPath(realPath);

        try {
            umf.transferTo(new File(absolPath));
            //把拍轮播图对象添加到数据库表里面
            cmfzArticle.setArticleImage(realPath);

            //测试是否从页面接收到数据
            //System.out.println(cmfzArticle);

            cmfzArticleService.updateArticle(cmfzArticle);
            map.put("isUpdate",true);
        } catch (IOException e) {
            e.printStackTrace();
            map.put("isUpdate",false);
        }
        return map;
    }


//    重置索引库
    @RequestMapping("/resetIndex")
    @ResponseBody
    public Map resetIndex(){
        return luceneService.resetIndex();
    }

//    根据关键词全文检索上师文章
    @RequestMapping("/selectArticleByKeyWord")
    @ResponseBody
    public Map selectArticleByKeyWord(int page,int rows,String keyWord){
        return luceneService.queryByKeyWord(page,rows,keyWord);
    }

}

